//
//  CollectionViewController.m
//  10-28
//
//  Created by ZF on 16/11/30.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "CollectionViewController.h"
#import "CustomCollectionViewCell.h"

@interface CollectionViewController ()<UICollectionViewDelegateFlowLayout>

@property (strong ,nonatomic) UIPageControl *pageView;


@end

@implementation CollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Register cell classes
    [self.collectionView registerClass:[CustomCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    self.collectionView.pagingEnabled = YES;
    self.collectionView.maximumZoomScale = 3;
    self.collectionView.minimumZoomScale = 1;
    
    UICollectionViewFlowLayout *layout = (UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout;
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    
    self.collectionView.contentOffset = CGPointMake(_idx * self.view.frame.size.width , 0);
    
    _pageView = [[UIPageControl alloc] init];
    _pageView.numberOfPages = _imgs.count;
    _pageView.currentPage = self.idx;
    _pageView.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height - 64);
  
    [self.view addSubview:_pageView];
    NSLog(@"图片有%ld张",self.imgs.count);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imgs.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.imgVi.image = _imgs[indexPath.item];
    //cell.imgVi.contentMode = UIViewContentModeScaleAspectFit;
    return cell;
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    NSLog(@"调用");
    return [[UIImageView alloc] initWithImage:_imgs[(int)scrollView.contentOffset.x / (int)self.view.frame.size.width]];
}

//停止滑动时调用此方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
}

//正在滑动时调用此方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int page = (int)scrollView.contentOffset.x  / (int)self.view.frame.size.width ;
    //NSLog(@"第%d页",page);
    self.pageView.currentPage = page;

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
